package global;

import heap.Heapfile;
import btree.*;
import bufmgr.*;
import diskmgr.*;
import catalog.*;
import tripleheap.*;
import labelheap.*;

public class SystemDefs {
	public static BufMgr	JavabaseBM;
	public static RdfDB	JavabaseDB;
	//public static Catalog	JavabaseCatalog;
	
	public static Heapfile tupleHeap;
	  public static TripleHeapfile tripleHeapStream;

	public static String  JavabaseDBName;
	public static String  JavabaseLogName;
	public static boolean MINIBASE_RESTART_FLAG = false;
	public static String	MINIBASE_DBNAME;

	public static TripleHeapfile tripleHeap;
	  public static LabelHeapFile entityLabelHeap;
	  public static LabelHeapFile predicateLabelHeap;
	  
	  public static BTreeFile predTIDBTIndex;
	 // public static BTreeFile confTIDBTIndex;
	  public static BTreeFile subTIDBTIndex;
	  public static BTreeFile objTIDBTIndex;
	  
	  public static BTreeFile compTID_SP_Index;
	  public static BTreeFile compTID_PO_Index;
	  public static BTreeFile compTID_SC_Index;
	  public static BTreeFile compTID_PC_Index;
	  public static BTreeFile compTID_OC_Index;
	  public static BTreeFile compTID_SO_Index;
	  
	  public static BTreeFile compTID_SPO_Index;
	  
	  
	  public static BTreeFileL entityLIDIndex;
	  public static BTreeFileL predicateLIDIndex;


	public SystemDefs (){};

	public SystemDefs(String dbname, int num_pgs, int bufpoolsize,
			String replacement_policy )
	{
		int logsize;

		String real_logname = new String(dbname);
		String real_dbname = new String(dbname);

		if (num_pgs == 0) {
			logsize = 500;
		}
		else {
			logsize = 3*num_pgs;
		}

		if (replacement_policy == null) {
			replacement_policy = new String("Clock");
		}

		init(real_dbname,real_logname, num_pgs, logsize,
				bufpoolsize, replacement_policy);
	}


	public void init( String dbname, String logname,
			int num_pgs, int maxlogsize,
			int bufpoolsize, String replacement_policy )
	{

		boolean status = true;
		JavabaseBM = null;
		JavabaseDB = null;
		JavabaseDBName = null;
		JavabaseLogName = null;
		tripleHeap=null;
		//JavabaseCatalog = null;

		try {
			JavabaseBM = new BufMgr(bufpoolsize, replacement_policy);
			JavabaseDB = new RdfDB();
			/*
	JavabaseCatalog = new Catalog(); 
			 */
		}
		catch (Exception e) {
			System.err.println (""+e);
			e.printStackTrace();
			Runtime.getRuntime().exit(1);
		}

		JavabaseDBName = new String(dbname);
		JavabaseLogName = new String(logname);
		MINIBASE_DBNAME = new String(JavabaseDBName);

		// create or open the DB

		if ((MINIBASE_RESTART_FLAG)||(num_pgs == 0)){//open an existing database
			try {
				JavabaseDB.openDB(dbname);
			}
			catch (Exception e) {
				System.err.println (""+e);
				e.printStackTrace();
				Runtime.getRuntime().exit(1);
			}
		} 
		else {
			try {
				JavabaseDB.openDB(dbname, num_pgs);
				JavabaseBM.flushAllPages();
			}
			catch (Exception e) {
				System.err.println (""+e);
				e.printStackTrace();
				Runtime.getRuntime().exit(1);
			}
		}
	}
}
